﻿using System.Collections.Generic;
using System.Linq;
using ARR.Entities;
using ARR.UI.CountryService;
using System.Windows.Input;
using ARR.UI.Commands;
using DDF.Entity;
using System;

namespace ARR.UI.ViewModels
{
    public class CountriesViewModel : ViewModelBase
    {
        private List<Country> countries;
        private Country selectedItem;
        private ICommand deleteSelectedItemsCommand;
        private ICommand deleteItemCommand;
        private ICommand saveChangesCommand;

        public CountriesViewModel()
        {
            LoadData();
        }

        public List<Country> Countries { get; set; }

        public Country SelectedItem
        {
            get { return selectedItem; }
            set { selectedItem = value; }
        }

        public ICommand DeleteSelectedItemsCommand
        {
            get
            {
                if (deleteSelectedItemsCommand == null)
                {
                    deleteSelectedItemsCommand = new RelayCommand(param => DeleteSelectedItems());
                }
                return deleteSelectedItemsCommand;
            }
        }

        public ICommand DeleteItemCommand
        {
            get
            {
                if (deleteItemCommand == null)
                {
                    deleteItemCommand = new RelayCommand(p => DeleteItem((int)p));
                }
                return deleteItemCommand;
            }
        }

        public ICommand SaveChangesCommand
        {
            get
            {
                if (saveChangesCommand == null)
                {
                    saveChangesCommand = new RelayCommand(param => SaveChanges());
                }
                return saveChangesCommand;
            }
        }

        private void DeleteItem(int countryId)
        {
            throw new System.NotImplementedException();
        }

        private void SaveChanges()
        {
            CountryServiceClient service = new CountryServiceClient();

            try
            {
                service.Save(countries);
                service.Close();

                LoadData();
            }
            catch (Exception ex)
            {
                service.Abort();
            }
        }

        private void DeleteSelectedItems()
        {
            SelectedItem.ChangeState(EntityState.Deleted);

            OnPropertyChanged<CountriesViewModel>(p => p.Countries);
        }

        private void LoadData()
        {
            CountryServiceClient service = new CountryServiceClient();

            try
            {
                Countries = new List<Country>();
                Countries.Add(new Country { Id = 1, Code = "C1", Name = "Country1" });
                Countries.Add(new Country { Id = 2, Code = "C2", Name = "Country2" });
                Countries.Add(new Country { Id = 3, Code = "C3", Name = "Country3" });
                Countries.Add(new Country { Id = 22, Code = "C4", Name = "Country4" });

                Article = new Article { Id = 1, Code = "AR01", EAN = "AR01", Name = "Articulo 1", CategoryId = 1, Country = Countries[0], Brand = "Brand", Manufacturer = "Manufacturer" };
            }
            catch (Exception ex)
            {
                service.Abort();
            }


        }


        public Article Article { get; set; }
    }
}
